package com.vitabuybuy.tv.ui.shopcenter.store.viewmodel

import androidx.databinding.ObservableArrayList
import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.BR
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.dto.*
import com.vitabuybuy.tv.inter.OnCouponCenterClickListener
import com.vitabuybuy.tv.inter.OnItemLiveClickListener
import com.vitabuybuy.tv.inter.OnItemPosterlickListener
import com.vitabuybuy.tv.network.BiddingRepository
import com.vitabuybuy.tv.network.CouponCenterRepository
import com.vitabuybuy.tv.network.GroupBuyRepository
import com.vitabuybuy.tv.network.StoreRepository
import com.vitabuybuy.tv.ui.tencent.liveroom.repository.LiveRepository
import me.tatarka.bindingcollectionadapter2.ItemBinding
import me.tatarka.bindingcollectionadapter2.OnItemBind

class StoreHomeViewModel : BaseViewModel() {

    val storeDtail = MutableLiveData<StroeDetailDto>()
    val liveDetils = MutableLiveData<LiveDteilaDto>()

    fun getStoreDetail(storeId: Int?) {
        rxHttpRequest {
            onRequest = {
                storeDtail.value = StoreRepository.getStoreDetail(storeId).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    val categoryList = MutableLiveData<MutableList<ClassificationDto>>()

    fun getClassification(storeId: Int?) {
        rxHttpRequest {
            onRequest = {
                categoryList.value = StoreRepository.getClassification(storeId).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    private val itemPosterListener = object : OnItemPosterlickListener {
        override fun onItemClick(item: BannerDto.ProductListBean, position: Int) {
            defUI.msgEvent.postValue(MessageModel(3, obj = item))
        }
    }

    val productList = MutableLiveData<MutableList<BannerDto.ProductListBean>>()

    val productBinding: OnItemBind<BannerDto.ProductListBean> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, item: BannerDto.ProductListBean ->
            itemBinding.set(BR.itemBean, R.layout.item_poster).bindExtra(BR.position, position)
                .bindExtra(BR.listenner, itemPosterListener)
        }


    private val itemOnClickListener = object : OnItemLiveClickListener {
        override fun onItemClick(item: LiveListDto, position: Int) {
            defUI.msgEvent.postValue(MessageModel(2, obj = item))
        }
    }

    val livelist = ObservableArrayList<LiveListDto>()

    //网格布局的adapter绑定
    val liveBinding: OnItemBind<LiveListDto> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, item: LiveListDto ->
            itemBinding.set(BR.itemBean, R.layout.item_playback).bindExtra(BR.position, position)
                .bindExtra(BR.listenner, itemOnClickListener)
        }

    fun getLiveList(
        storeId: Int?,
        pageNumber: Int,
        pageSize: Int
    ) {
        rxHttpRequest {
            onRequest = {
                val it = StoreRepository.getLiveList(storeId, pageNumber, pageSize).await()
                if (pageNumber == 1) {
                    livelist.clear()
                }
                livelist.addAll(it)
                if (livelist.isEmpty()) {
                    statusView.set(LoadingType.SHOW_EMPTY)
                }else{
                    statusView.set(LoadingType.SHOW_CONTENT)
                }
                defUI.msgEvent.postValue(MessageModel(1, arg1 = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }


    val groupGoodslist = MutableLiveData<MutableList<GroupBuyListDto>>()

    fun getGroupGoodsList(storeId: Int?, groupType: String, pageNumber: Int) {
        val map = HashMap<String, Any>()
        map["store"] = mapOf(Pair("id", storeId))
        if (groupType.isNotEmpty()) map["groupBuying"] = mapOf(Pair("type", groupType))
        rxHttpRequest {
            onRequest = {
                groupGoodslist.value = GroupBuyRepository.getGroupBuyList(map, pageNumber).await()
                groupGoodslist.value?.run {
                    defUI.msgEvent.postValue(MessageModel(1, arg1 = this.size))
                }
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }


    val biddingGoodslist = MutableLiveData<MutableList<BiddingListDto>>()

    fun getBiddingGoodsList(storeId: Int?, groupType: String, pageNumber: Int) {
        val map = HashMap<String, Any?>()
        map["storeId"] = storeId
        if (groupType.isNotEmpty()) map["status"] = groupType
        rxHttpRequest {
            onRequest = {
                biddingGoodslist.value = BiddingRepository.getBiddingList(map, pageNumber).await()
                biddingGoodslist.value?.run {
                    defUI.msgEvent.postValue(MessageModel(1, arg1 = this.size))
                }
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }

    private val itemCouponClickListener = object : OnCouponCenterClickListener {
        override fun onItemClick(item: CouponCenterDto, position: Int) {
            defUI.msgEvent.postValue(MessageModel(3, arg1 = position, obj = item))
        }
    }

    val couponList = ObservableArrayList<CouponCenterDto>()
    val couponBinding: OnItemBind<CouponCenterDto> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, _: CouponCenterDto ->
            itemBinding.set(BR.itemBean, R.layout.item_coupons_1).bindExtra(BR.position, position)
                .bindExtra(BR.listenner, itemCouponClickListener)
        }

    fun getCouponList(pageNumber: Int, storeId: Int?) {
        val map = HashMap<String, Any?>()
        map["store"] = mapOf(Pair("id", storeId))
        rxHttpRequest {
            onRequest = {
                val it = CouponCenterRepository.getCouponsList(map, pageNumber).await()
                if (pageNumber == 1) {
                    couponList.clear()
                }
                couponList.addAll(it)
                if (couponList.isEmpty()) {
                    statusView.set(LoadingType.SHOW_EMPTY)
                }else{
                    statusView.set(LoadingType.SHOW_CONTENT)
                }
                defUI.msgEvent.postValue(MessageModel(1, arg1 = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }

    fun saveACoupon(couponId: Int, position: Int) {
        rxHttpRequest {
            onRequest = {
                CouponCenterRepository.saveACoupon(couponId).await()
                defUI.msgEvent.postValue(MessageModel(4, arg1 = position))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }


    fun collectOrcancel(id: Int?, isFav: Boolean?) {
        rxHttpRequest {
            onRequest = {
                StoreRepository.collectOrcancelStore(id, isFav).await()
                defUI.msgEvent.postValue(MessageModel(1, obj = isFav))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }
    fun getLiveDetail(liveId: Int?) {
        rxHttpRequest {
            onRequest = {
                liveDetils.value = LiveRepository.getLiveDetail(liveId).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }
}
